<?php
namespace app\mouth\model;
use think\Db;
use app\mouth\common\Encapsulation;
class UserModel extends Encapsulation{

    public function contenta($openid,$vu_id){
        $limi['openid'] = $openid;
        $limi['vu_id'] = $vu_id;
       return $this->one('vehicle_user_account',$limi,'*');
    }

    public function measure($data){
        if (empty($data['openid']) || empty($data['vu_id']) || empty($data['brand_model']) || empty($data['frame_number']) || empty($data['engine_number']) || empty($data['maintain']) || empty($data['Insurance']) || empty($data['yearly_inspection'])){
            return false;
        }
        $date['openid'] = $data['openid'];
        $date['vu_id'] = $data['vu_id'];
        $date['brand_model'] = $data['brand_model'];
        $date['frame_number'] = $data['frame_number'];
        $date['engine_number'] = $data['engine_number'];
        $date['maintain'] = strtotime($data['maintain']);
        $date['Insurance'] = strtotime($data['Insurance']);
        $date['yearly_inspection'] = strtotime($data['yearly_inspection']);
        return $date;
    }

    public function phenomenon($openid,$parameter,$ints=1){
        $package['openid'] = $openid;
        $package['brand_model'] = $parameter['brand_model'];
        $package['vu_id'] = $parameter['vu_id'];

        if ($ints === 1){
            $package['time'] = $parameter['Insurance'];
            $this->insert('vehicle_user_account_insurance',$package);

            unset($package['time']);
            $package['time'] = $parameter['maintain'];
            $this->insert('vehicle_user_account_maintain',$package);

            unset($package['time']);
            $package['time'] = $parameter['yearly_inspection'];
            $this->insert('vehicle_user_account_yearly_inspection',$package);
        }else{
            $where['openid'] = $openid;
            $where['vu_id'] = $package['vu_id'];

            $package['time'] = $parameter['Insurance'];
            $this->up('vehicle_user_account_insurance',$where,$package);

            unset($package['time']);
            $package['time'] = $parameter['maintain'];
            $this->up('vehicle_user_account_maintain',$where,$package);

            unset($package['time']);
            $package['time'] = $parameter['yearly_inspection'];
            $this->up('vehicle_user_account_yearly_inspection',$where,$package);
        }
    }
    public function content($data,$openid){
        if ($this->contenta($openid,$data['vu_id'])){
            return false;
        }
        $parameter = $this->measure($data);

        if ($parameter){
            $this->phenomenon($openid,$parameter,1);
            return $this->insert('vehicle_user_account',$parameter);
        }
        return false;
    }

    public function contents($data,$openid){
        $add = $this->contenta($openid,$data['vu_id']);
        if (empty($add)){
            return false;
        }
        if (empty($data['brand_model'])){
            return $add;
        }
        $parameter = $this->measure($data);
        if ($parameter){
            $this->phenomenon($openid,$parameter,2);
            $where['openid'] = $parameter['openid'];
            $where['vu_id'] = $parameter['vu_id'];
            return $this->up('vehicle_user_account',$where,$parameter);

        }
        return false;
    }




    public function appear($openid){
        return $this->one('certificates',['openid'=>$openid],'*');
    }
    public function certificatesadd($add,$openid){
        $res = $this->appear($openid);
        if ($res){
           return $this->up('certificates',['openid'=>$openid],$add);
        }
        $add['openid']=$openid;
        return $this->insert('certificates',$add);
    }
    public function protect($openid,$vu_id){
       $where['openid']=$openid;
       $where['vu_id']=$vu_id;
      $array = $this->one('vehicle_user_account',$where,'*');
      if (empty($array)){
          return false;
      }
//        $array['maintain'] = ceil((strtotime($array['maintain'])-time())/86400);
//        $array['Insurance'] = ceil((strtotime($array['Insurance'])-time())/86400);
//        $array['yearly_inspection'] = ceil((strtotime($array['yearly_inspection'])-time())/86400);
        return $array;
    }
    public function index($openid){
        $user = Db::name('wx_user')->where('openid',$openid)->find();
        if ($user['mobile']){
            $shop = Db::name('store')->field('ma_id,ma_name')->where('ma_number',$user['mobile'])->find();
            if ($shop){
                $date['shop'] = $shop;
                $date['user'] = $user;
            } else {
                $date['shop'] = '';
                $date['user'] = $user;
            }
        } else {
            $date['shop'] = '';
            $date['user'] = $user;
        }
        return $date;
    }

    public function addMobileNumber($openid,$number){
        Db::name('wx_user')->where('openid',$openid)->update(['mobile'=>$number]);
    }
    public function qa_question($qc_id){
        if ($qc_id == null){
            $date['classification'] = Db::name('qa_category')->field('qc_id,qc_name,time')->order('qc_displayorder desc')->select();
            $date['problem'] = Db::name('qa_question')->order('qq_id desc')->select();
            return $date;
        }else{
           return Db::name('qa_question')->where('qq_cate_id',$qc_id)->order('qq_id desc')->select();
        }

    }
    public function myanswer($openid){
        return Db::name('qa_reply')->where('openid',$openid )->order('id desc')->select();
    }
    public function deletequestions($id){
        Db::name('qa_question_copy')->where('qq_id',$id)->delete();
        Db::name('qa_question')->where('qq_id',$id)->delete();
        Db::name('qa_reply')->where('qq_id',$id)->delete();
       return true;
    }
    public function sensitivecharacters($str){
        $allergicWord  = (include 'SensitiveThesaurus.php');
        for ($i=0;$i<count($allergicWord);$i++){
            $content = substr_count($allergicWord[$i],$str);
            if($content>0){
                return $content;
            }
        }
    }
    public function questionanswering($date,$openid,$nicknime,$portrait){
        if (empty($date['other_answer'])){
            return '回答不能为空';
        }
        $str = $date['other_answer'];
        $info = $this->sensitivecharacters($str);
        if($info>0){
            return '输入内容涉及敏感字符';
        }
        $value['other_answer'] = $date['other_answer'];
        $value['other_openid'] = $date['other_openid'];
        $value['other_nickname'] = $date['other_nickname'];
        $value['other_portrait'] = $date['other_portrait'];
        $value['answer'] = $date['answer'];
        $value['user_openid'] = $openid;
        $value['user_portrait'] = $portrait;
        $value['user_nickname'] = $nicknime;
        $value['qq_id'] = $date['qq_id'];
        $value['time'] = time();
        $type = Db::name('qa_question_copy')->insert($value);
        if (!$type){
            return '添加失败';
        }
        $content['openid'] = $openid;
        $content['nickname'] = $nicknime;
        $content['portrait'] = $portrait;
        $content['qq_id'] = $date['qq_id'];
        $content['other_answer'] = $date['other_answer'];
        $content['answer'] = $date['answer'];
        $content['time'] = time();
        $res = Db::name('qa_reply')->insert($content);
        if (!$res){
            return '添加失败';
        }
        return true;

    }

    public function putquestionsto($date,$nickname,$portrait){
        if (empty($date['answer'])){
            return '问题不能为空';
        }
        $str = $date['answer'];
        $info = $this->sensitivecharacters($str);
        if($info>0){
            return '输入内容涉及敏感字符';
        }
        $value['qq_openid']     = $date['openid'];
        $value['qq_cate']       = $date['qq_cate'];
        $value['portrait']      = $portrait;
        $value['nickname']      = $nickname;
        $value['time']          = time();
        $value['answer']        = $date['answer'];
        $value['qq_cate_id']    = $date['qq_cate_id'];
        $type = Db::name('qa_question')->insert($value);
        if ($type){
            return true;
        }
        return '添加失败';
    }
    public function questiondetails($id){
       $date['problem'] = Db::name('qa_question')->where('qq_id',$id)->order('time desc')->select();
       $date['answer'] = Db::name('qa_question_copy')->where('qq_id',$id)->order('time desc')->select();
        return $date;
    }
    public function myquestion($openid){
        return Db::name('qa_question')->where('qq_openid',$openid)->order('qq_id desc')->select();
    }
    public function userOpenid($openid){
       return Db::name('vehicle_user')->where('openid',$openid)->order('id desc')->select();
    }
    public function volume($openid,$evaluate){
        $where['or_openid'] = $openid;
        switch ($evaluate){
            case 1:
                $where['or_status'] = 1;
            break;
            case 2:
                $where['or_status'] = 2;
            break;
            case 3:
                $where['or_status'] = 3;
            break;
            case 4:
                $where['or_status'] = 4;
                break;
            case '':
                break;
            default:
                $where['evaluate'] = 1;
        }
       $add =  Db::name('order')->where('or_openid',$openid)->whereTime('s_time', 'last week')->order('or_id desc')->select();
        var_dump($add);
        die;
     return Db::name('order')->where($where)->order('or_id desc')->select();
    }
    public static function discount($userid,$type){
        if ($type){
            $where['a.state']  = $type;
        }
        $where['a.user_id'] = $userid;
        return Db::table('yc_wx_user_discount')
            ->alias('a')
            ->join('yc_discount w','a.volume_id = w.id')
            ->where($where)
            ->select();
    }
}
